package com.aat.dxfy.lucene.service.impl;

import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.apache.lucene.analysis.standard.StandardAnalyzer;
import org.apache.lucene.document.Document;
import org.apache.lucene.document.Field;
import org.apache.lucene.document.TextField;
import org.apache.lucene.index.DirectoryReader;
import org.apache.lucene.index.IndexWriter;
import org.apache.lucene.index.IndexWriterConfig;
import org.apache.lucene.index.IndexWriterConfig.OpenMode;
import org.apache.lucene.queryparser.classic.MultiFieldQueryParser;
import org.apache.lucene.search.BooleanClause;
import org.apache.lucene.search.IndexSearcher;
import org.apache.lucene.search.Query;
import org.apache.lucene.search.ScoreDoc;
import org.apache.lucene.search.TopDocs;
import org.apache.lucene.store.Directory;
import org.apache.lucene.store.FSDirectory;

import com.aat.dxfy.lucene.bean.Article;
import com.aat.dxfy.lucene.dao.IArticleDAO;
import com.aat.dxfy.lucene.service.IArticleService;

public class ArticleServiceImpl implements IArticleService {

	private IArticleDAO articleDAO;

	private final String INDEXPATH = "D:\\index";

	public List<Article> getArticles(String query) {

		try {
			List<Article> qlist = new ArrayList<Article>();

			Directory dir = FSDirectory.open(Paths.get(INDEXPATH));
			IndexSearcher indexSearcher = new IndexSearcher(DirectoryReader.open(dir));

			long begin = new Date().getTime();

			// 下面的是进行title,content 两个范围内进行收索.
			BooleanClause.Occur[] clauses = { BooleanClause.Occur.SHOULD, BooleanClause.Occur.SHOULD };

			Query queryOBJ = MultiFieldQueryParser.parse(query, new String[] { "title", "content" }, clauses,
					new StandardAnalyzer());
			TopDocs topDocs = indexSearcher.search(queryOBJ, 1000);

			// 输出结果
			for (ScoreDoc scoreDoc : topDocs.scoreDocs) {
				if (scoreDoc.score > 10) {
					Document targetDoc = indexSearcher.doc(scoreDoc.doc);

					Article article = new Article();

					String title = targetDoc.get("title");
					String content = targetDoc.get("content");

					article.setTitle(title);
					article.setContent(content);
					article.setTag(targetDoc.get("tag"));
					article.setTotalHits(topDocs.totalHits);

					qlist.add(article);
				}
			}

			long end = new Date().getTime();
			System.out.println(">>> 3.搜索完毕... ... 共花费：" + (end - begin) + "毫秒...");

			return qlist;

		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}

	public boolean createIndex() {
		List<Article> list = articleDAO.getArticles();

		try {
			Directory directory = FSDirectory.open(Paths.get(INDEXPATH));
			IndexWriterConfig config = new IndexWriterConfig(new StandardAnalyzer());
			config.setOpenMode(OpenMode.CREATE_OR_APPEND);
			IndexWriter indexWriter = new IndexWriter(directory, config);

			long begin = new Date().getTime();
			for (Article art : list) {
				Document doc = new Document();
				String title = art.getTitle() == null ? "" : art.getTitle().trim();
				String content = art.getContent() == null ? "" : art.getContent();
				String tag = art.getTag() == null ? "" : art.getTag();
				doc.add(new Field("title", title, TextField.TYPE_STORED));
				doc.add(new Field("content", content, TextField.TYPE_STORED));
				doc.add(new Field("tag", tag, TextField.TYPE_STORED));
				indexWriter.addDocument(doc);
			}
			long end = new Date().getTime();
			System.out.println(">>> 1.存入索引完毕.. 共花费：" + (end - begin) + "毫秒...");

			indexWriter.commit();
			indexWriter.close();
			return true;

		} catch (Exception e) {
			e.printStackTrace();
			return false;
		}
	}

	public void setArticleDAO(IArticleDAO articleDAO) {
		this.articleDAO = articleDAO;
	}

}